<?php

namespace App\Admin\Forms\sms;

use Dcat\Admin\Widgets\Form;

class SmsForm extends Form
{
    use \App\Services\SmsService;

    public function handle(array $input)
    {
        $phones  = $input['phones'];
        $content = $input['content'];
        if (!$phones)
            return $this->response()->error('请输入手机号');
        if (!$content)
            return $this->response()->error('请输入发送内容');

        foreach ($phones as $key => $v) {
            if (!is_mobile($v))
                return $this->response()->error('第' . ($key + 1) . '个手机号错误');
        }

        $this->response()->success('发送中');


        // $mailLogData = [];
        foreach ($phones as $key => $v) {
            $action = 'code';
            $templateParam = ['code' => rand(100000, 999999)];
            $result = $this->sendSms($v, $action, $templateParam);
            if ($result['code'] == 0) {
                return $this->response()->error($result['msg']);
            }else{
                if($result['data']->body->code != 'OK'){
                    return $this->response()->error($result['data']->body->message);
                }
            }
            //    Mail::to($v)->queue(new OrderShipped($input['title'], $input['content']));

            //     $mailLogData[] = [
            //         'title'        => $input['title'],
            //         'from_address' => $input['from_address'],
            //         'email'        => $v,
            //         'content'      => $input['content'],
            //         'operator'     => Admin::user()->username,
            //         'created_at'   => Carbon::now(config('app.timezone'))->format(CarbonInterface::DEFAULT_TO_STRING_FORMAT)
            //     ];

        }

        // Log::query()->insert($mailLogData);

        return $this->response()->success('发送成功');
    }

    public function form()
    {
        $this->list('phones', '手机号')->setLabelClass(['asterisk']);
        $this->textarea('content', '内容')->setLabelClass(['asterisk']);
    }
}
